home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / dasv10_.arj / HEXDUMP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-29  |  1.2 KB  |  54 lines

  1. // hexdump.cpp : Program producing hexdump of input file
  2. #include <fstream.h>
  3. #include <iomanip.h>
  4. #include <ctype.h>
  5.  
  6. main(int argc, char *argv[])
  7. {
  8.     istream *fin = &cin;   // default input is cin
  9.     ostream *fout = &cout; // default output is cout
  10.     fstream infile, outfile;
  11.  
  12.     if (argc > 1) { // input file specified
  13.         infile.open(argv[1], ios::in | ios::binary);
  14.         if (!infile) {
  15.             cerr << "Error opening input file " << argv[1] << '\n';
  16.             return 1;
  17.         }
  18.         fin = &infile;
  19.     }
  20.  
  21.     char ascii_array[17]; // ascii portion of dump
  22.     ascii_array[16] = 0; // make it null terminated
  23.     int nextc = 0;
  24.  
  25.     // set up for uppercase hex output with '0' fill
  26.     *fout << setiosflags(ios::uppercase) << hex << setfill('0');
  27.  
  28.     // loop till end of file
  29.     while(1) {
  30.         int c = fin->get();
  31.         if (c == EOF) break;
  32.         *fout << setw(2) << c << ' ';
  33.         if (!isprint(c)) c = '.'; // convert nonprintable chars to '.'
  34.         ascii_array[nextc++] = c;
  35.         if (nextc == 16) {
  36.             *fout << ascii_array << '\n';
  37.             nextc = 0;
  38.         }
  39.     }
  40.     // dump out left overs
  41.     if (nextc) {
  42.         for (; nextc<16; nextc++) {
  43.             *fout << "00 ";
  44.             ascii_array[nextc] = '.';
  45.         }
  46.         *fout << ascii_array << '\n';
  47.     }
  48. return 0;
  49. }
  50.  
  51.  
  52.  
  53.  
  54.